extends Node2D

var game_scene
@onready var person: Label = $panel/person
@onready var word: Label = $panel/word
@onready var choice_list: VBoxContainer = $choice_list
@onready var mask: ColorRect = $mask
@onready var cg: Sprite2D = $CG
@onready var sound: AudioStreamPlayer2D = $sound

var is_chatting=false
var waiting=false

var typing_tween:Tween

func _ready() -> void:
	game_scene=get_tree().get_first_node_in_group("game_scene")

func _current():
	if global.cur_index>=len(global.dialog_list[global.cur_line]):
		global.cur_index=len(global.dialog_list[global.cur_line])-1
	return global.dialog_list[global.cur_line][global.cur_index]

func _next():
	person.text=_current()["person"]+":"
	if typing_tween and typing_tween.is_running():
		typing_tween.kill()
		word.text=_current()["word"]
		global.cur_index+=1
	else:
		sound.play()
		typing_tween=get_tree().create_tween()
		word.text=""
		for char in _current()["word"]:
			typing_tween.tween_callback(_add_char.bind(char)).set_delay(0.05)
		typing_tween.tween_callback(func(): global.cur_index+=1)
		
func _add_char(char:String):
	word.text+=char

func _end(reset:bool=true):
	if reset:
		global.cur_index=0
	game_scene._chat_out()

func _jump_to():
	var link={"line":_current()["line"],"index":_current()["index"]}
	global.cur_line=link["line"]
	global.cur_index=link["index"]
	_continue()

func _jump_end():
	var link={"line":_current()["line"],"index":_current()["index"]}
	global.cur_line=link["line"]
	global.cur_index=link["index"]
	_end(false)

func _choice():
	waiting=true
	choice_list.visible=true
	for i in range(_current()["num"]):
		var new_choice=preload("res://node/choice.tscn").instantiate()
		new_choice.word=_current()["word"][i]
		new_choice._set_word()
		new_choice.fn=_current()["fn"][i]
		choice_list.add_child(new_choice)

func _choice_end():
	waiting=false
	choice_list.visible=false
	var choice=choice_list.get_children()
	for i in choice:
		i.queue_free()
	_continue()

func _fade_in():
	mask.visible=true
	mask._fade_in().finished
	global.cur_index+=1

func _fade_out():
	global.cur_index+=1
	await mask._fade_out().finished
	mask.visible=false

func _trigger():
	global.flag_list[_current()["flag"]]+=1
	global.cur_index+=1
	_continue()

func _change():
	if _current()["level"]>global.cur_checkpoint:
		global.cur_checkpoint=_current()["level"]
	global.cur_index+=1
	_continue()

func _image():
	if _current()["image"]!="clear":
		cg.visible=true
		cg.texture=load(_current()["image"])
	else:
		cg.visible=false
	global.cur_index+=1
	_continue()

func _audio():
	global.BGM.stream=load(_current()["audio"])
	global.BGM.play()
	global.cur_index+=1
	_continue()

func _finale():
	get_tree().change_scene_to_file("res://scene/finale.tscn")

func _continue():
	match _current()["conti"]:
			"next":
				_next()
			"end":
				_end()
			"jump_to":
				_jump_to()
			"jump_end":
				_jump_end()
			"choice":
				_choice()
			"fade_in":
				_fade_in()
			"fade_out":
				_fade_out()
			"trigger":
				_trigger()
			"change":
				_change()
			"image":
				_image()
			"audio":
				_audio()
			"finale":
				_finale()

func _physics_process(delta: float) -> void:
	if Input.is_action_just_pressed("conti"):
		if not is_chatting:
			return
		if waiting:
			return
		_continue()
	elif Input.is_action_pressed("skip"):
		if not is_chatting:
			return
		if waiting:
			return
		_continue()
